package com.shop.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.*;
import com.shop.service.PhoneService;
import com.shop.service.ShoppingCarService;
import com.shop.service.UserOrderService;
import com.shop.service.UserService;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.LocaleData;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-08
 */
@RestController
@RequestMapping("/shopping-car")
public class ShoppingCarController {
    @Autowired
    ShoppingCarService shoppingCarService;
    @Autowired
    PhoneService phoneService;
    @Autowired
    UserService userService;
    @Autowired
    UserOrderService userOrderService;
    @PostMapping("add")
    public Res add(@RequestBody HashMap<String,String> params) {
        String userName = params.get("userName");
        String phoneId = params.get("phoneId");
        Phone phone = phoneService.getById(phoneId);
        QueryWrapper<ShoppingCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName).eq("phone_id",phoneId);
        ShoppingCar one = shoppingCarService.getOne(queryWrapper);
        if(one == null) {

            ShoppingCar shoppingCar = new ShoppingCar();
            shoppingCar.setUserName(userName);
            shoppingCar.setColor(phone.getColor());
            shoppingCar.setDisk(phone.getDisk().toString());
            shoppingCar.setMemory(phone.getMemory().toString());
            shoppingCar.setPrice(phone.getPrice());
            shoppingCar.setId(UUID.randomUUID().toString());
            shoppingCar.setPhoneName(phone.getPhoneName());
            shoppingCar.setPhoneId(phone.getId());
            boolean save = shoppingCarService.save(shoppingCar);
            return save ? Res.success() : Res.error();
        }
        return Res.success();
    }
    @GetMapping("list")
    public Res list ( String userName){
        QueryWrapper<ShoppingCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        List<ShoppingCar> list = shoppingCarService.list(queryWrapper);
        return Res.success(list);
    }

    @PostMapping("del")
    public Res del (@RequestBody HashMap<String ,String> params) {
        String id = params.get("id");
        boolean b = shoppingCarService.removeById(id);
        return b?Res.success():Res.error();
    }

    @PostMapping("buy")
    public Res buy (@RequestBody HashMap<String ,Object> params) {
        String userName = (String) params.get("userName");
        List<ShoppingCar> list = (List<ShoppingCar>) params.get("phones");

        User user = userService.getOne(new QueryWrapper<User>().eq("user_name", userName));

        Float sum = 0F;
        List<UserOrder> orderList = new ArrayList<>();
        List<String > ids = new ArrayList<>(list.size());
        for(Object object:list){
            // 将list中的数据转成json字符串
            String jsonObject= JSON.toJSONString(object);
            //将json转成需要的对象
            ShoppingCar shoppingCar= JSONObject.parseObject(jsonObject,ShoppingCar.class);
            String  id = shoppingCar.getPhoneId();
            Phone phone = phoneService.getById(id);
            ids.add(shoppingCar.getId());
            sum = sum + shoppingCar.getPrice();
            UserOrder order = new UserOrder();
            order.setDisk(shoppingCar.getDisk());
            order.setMemory(shoppingCar.getMemory());
            order.setPrice(shoppingCar.getPrice());
            order.setPhoneId(shoppingCar.getPhoneId());
            order.setPhoneName(shoppingCar.getPhoneName());
            order.setUserName(userName);
            order.setCreateTime(new Date());
            order.setStatus(UserOrderService.NOT_COMMENT);
            order.setPhoneId(shoppingCar.getPhoneId());
            order.setId(UUID.randomUUID().toString());
            order.setLogistics(UserOrderService.NOT_SEND);
            order.setAddress(user.getAddress());
            order.setCoverSrc(phone.getCoverSrc());
            orderList.add(order);
        }
        if(sum > user.getAccount()){
            return Res.error("资金不够哦");
        }
        user.setAccount(user.getAccount() - sum);
        boolean update = userService.update(user, new QueryWrapper<User>().eq("user_name", userName));
        boolean remove = shoppingCarService.removeByIds(ids);
        boolean saveBatch = userOrderService.saveBatch(orderList);
        return update && remove && saveBatch ? Res.success():Res.error("出错啦");
//        return Res.error();
    }

}

